package org.fuys.coder.application.config;

import org.fuys.coder.application.interceptor.LoginInterceptor;
import org.fuys.coder.infrastructure.util.RedisUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.common.config.sys
 * @className: WebConfig
 * @author: WangJing
 * @description: 关于跨域等问题的配置
 * @date: 2024/6/21 16:27
 * @version: 1.0
 */

@Configuration
@EnableAspectJAutoProxy
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private RedisUtil redisUtil;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //todo 有些路径是测试时避过的 如文件相关的全部
        registry.addInterceptor(new LoginInterceptor(redisUtil))
                .addPathPatterns("/fuyscoder/**")
                .excludePathPatterns("/fuyscoder/index"
                        ,"/fuyscoder/auth/**","/fuyscoder/recommend/default",
                        "/fuyscoder/file/other","/fuyscoder/file/media","/fuyscoder/file/multi");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        String[] url={"http://127.0.0.1:8081","http://localhost:8081"};
        registry.addMapping("/**")
                .allowedOrigins(url)
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowCredentials(true)
                .exposedHeaders("token","Authorization");
    }
}
